﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace DSZI
{
    public partial class Wants : Window
    {
        private static string startCity;

        public Wants(string _startCity)
        {
            SourceInitialized += MainWindow_SourceInitialized;
            InitializeComponent();
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

            startCity = _startCity;

            FillCities();
            FillSights();

            if (Memory.instance.UserWantsSomething())
            {
                string[] wants = new string[2] { Memory.instance.RemindWhatUserWants()[0], Memory.instance.RemindWhatUserWants()[1] };
                if (wants[0] != null) comboBoxCities.Text = wants[0];
                if (wants[1] != null) comboBoxSights.Text = wants[1];
            }
        }

        #region NO CLOSE BUTTON
        private const int GWL_STYLE = -16;
        private const int WS_SYSMENU = 0x80000;
        [DllImport("user32.dll", SetLastError = true)]
        private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        void MainWindow_SourceInitialized(object sender, EventArgs e)
        {
            WindowInteropHelper wih = new WindowInteropHelper(this);
            int style = GetWindowLong(wih.Handle, GWL_STYLE);
            SetWindowLong(wih.Handle, GWL_STYLE, style & ~WS_SYSMENU);
        }
        #endregion

        #region TRANSPARENT BACKGROUND
        [StructLayout(LayoutKind.Sequential)]
        private struct MARGINS
        {
            public int cxLeftWidth;      // width of left border that retains its size
            public int cxRightWidth;     // width of right border that retains its size
            public int cyTopHeight;      // height of top border that retains its size
            public int cyBottomHeight;   // height of bottom border that retains its size
        };

        [DllImport("DwmApi.dll")]
        private static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS pMarInset);

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            WindowInteropHelper windowInteropHelper = new WindowInteropHelper(this);
            IntPtr myHwnd = windowInteropHelper.Handle;
            HwndSource mainWindowSrc = System.Windows.Interop.HwndSource.FromHwnd(myHwnd);

            mainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);

            MARGINS margins = new MARGINS()
            {
                cxLeftWidth = -1,
                cxRightWidth = -1,
                cyBottomHeight = -1,
                cyTopHeight = -1
            };

            DwmExtendFrameIntoClientArea(myHwnd, ref margins);
        }
        #endregion

        private void FillCities()
        {
            comboBoxCities.Items.Clear();
            foreach (string city in MainWindow.cities)
            {
                if (!city.Equals(startCity)) //nie jest miastem początkowym
                {
                    if (!comboBoxSights.Text.StartsWith("[" + city + "]")) //nie jest miastem, z którego wcześniej wybrano jakiś obiekt - drugi comboBox
                    {
                        if (Memory.instance.UserHatesSomething()) //użytkownik zaznaczył, że nie chce czegoś widzieć (Hates.xaml) - trzeba to usunąć z listy
                        {
                            string[] hates = new string[2] { Memory.instance.RemindWhatUserHates()[0], Memory.instance.RemindWhatUserHates()[1] };
                            if (hates[0] != null && hates[1] == null) //użytkownik nie chce widzieć jakiegoś miasta
                            {
                                if (!city.Equals(hates[0]))
                                {
                                    comboBoxCities.Items.Add(city);
                                }
                            }
                            if (hates[0] == null && hates[1] != null) //użytkownik nie chce widzieć jakiegoś obiektu
                            {
                                if (!hates[1].StartsWith("[" + city + "]"))
                                {
                                    comboBoxCities.Items.Add(city);
                                }
                            }
                            if (hates[0] != null && hates[1] != null) //użytkownik nie chce widzieć jakiegoś miasta i obiektu
                            {
                                if (!city.Equals(hates[0]) &&
                                    !hates[1].StartsWith("[" + city + "]"))
                                {
                                    comboBoxCities.Items.Add(city);
                                }
                            }
                        }
                        else comboBoxCities.Items.Add(city);
                    }
                }
            }
        }

        private void FillSights()
        {
            comboBoxSights.Items.Clear();
            foreach (string sight in MainWindow.sights)
            {
                if (!sight.StartsWith("[" + startCity + "]")) //nie jest obiektem z miasta początkowego
                {
                    if (!sight.StartsWith("[" + comboBoxCities.Text + "]")) //nie jest obiektem, z wybranego wcześniej miasta - pierwszy comboBox
                    {
                        if (Memory.instance.UserHatesSomething()) //użytkownik zaznaczył, że nie chce czegoś widzieć (Hates.xaml) - trzeba to usunąć z listy
                        {
                            string[] hates = new string[2] { Memory.instance.RemindWhatUserHates()[0], Memory.instance.RemindWhatUserHates()[1] };
                            if (hates[0] != null && hates[1] == null) //użytkownik nie chce widzieć jakiegoś miasta
                            {
                                if (!sight.StartsWith("[" + hates[0] + "]"))
                                {
                                    comboBoxSights.Items.Add(sight);
                                }
                            }
                            if (hates[0] == null && hates[1] != null) //użytkownik nie chce widzieć jakiegoś obiektu
                            {
                                #region TRIM
                                string temp = hates[1].Substring(1);
                                string city = null;
                                for (int i = 0; i < temp.Length; i++)
                                {
                                    if (temp[i] == ']')
                                    {
                                        city = temp.Remove(i);
                                        break;
                                    }
                                }
                                #endregion
                                if (!sight.StartsWith("[" + city + "]"))
                                {
                                    comboBoxSights.Items.Add(sight);
                                }
                            }
                            if (hates[0] != null && hates[1] != null) //użytkownik nie chce widzieć jakiegoś miasta i obiektu
                            {
                                #region TRIM
                                string temp = hates[1].Substring(1);
                                string city = null;
                                for (int i = 0; i < temp.Length; i++)
                                {
                                    if (temp[i] == ']')
                                    {
                                        city = temp.Remove(i);
                                        break;
                                    }
                                }
                                #endregion
                                if (!sight.StartsWith("[" + hates[0] + "]") &&
                                    !sight.StartsWith("[" + city + "]"))
                                {
                                    comboBoxSights.Items.Add(sight);
                                }
                            }
                        }
                        else comboBoxSights.Items.Add(sight);
                    }
                }
            }
        }

        private void CitiesChecker(object sender, EventArgs e)
        {
            FillCities();
        }

        private void SightsChecker(object sender, EventArgs e)
        {
            FillSights();
        }

        private void buttonOk_Click(object sender, MouseButtonEventArgs e)
        {
            string city = null;
            string sight = null;

            if (!comboBoxCities.Text.Equals("")) city = comboBoxCities.Text;
            if (!comboBoxSights.Text.Equals("")) sight = comboBoxSights.Text;

            if (city != null || sight != null)
            {
                string[] wants = new string[2] { city, sight };
                Memory.instance.RememberUserWantsThis(wants);
            }

            Close();
        }

        private void buttonNo_Click(object sender, MouseButtonEventArgs e)
        {
            Memory.instance.ForgetWhatUserWants();
            Close();
        }

        #region VISUALS

        private void MouseOverNo(object sender, MouseEventArgs e)
        {
            ImageBrush img = new ImageBrush();
            img.ImageSource = (ImageSource)new ImageSourceConverter().ConvertFromString("..\\..\\Resources\\Buttons\\NoOver.png");
            buttonNo.Source = img.ImageSource;
        }

        private void MouseLeaveNo(object sender, MouseEventArgs e)
        {
            ImageBrush img = new ImageBrush();
            img.ImageSource = (ImageSource)new ImageSourceConverter().ConvertFromString("..\\..\\Resources\\Buttons\\No.png");
            buttonNo.Source = img.ImageSource;
        }

        private void MouseOverOk(object sender, MouseEventArgs e)
        {
            ImageBrush img = new ImageBrush();
            img.ImageSource = (ImageSource)new ImageSourceConverter().ConvertFromString("..\\..\\Resources\\Buttons\\OkOver.png");
            buttonOk.Source = img.ImageSource;
        }

        private void MouseLeaveOk(object sender, MouseEventArgs e)
        {
            ImageBrush img = new ImageBrush();
            img.ImageSource = (ImageSource)new ImageSourceConverter().ConvertFromString("..\\..\\Resources\\Buttons\\Ok.png");
            buttonOk.Source = img.ImageSource;
        }

        #endregion
    }
}
